package com.funsport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.funsport.entity.PointProduct;
import com.funsport.mapper.PointProductMapper;
import com.funsport.service.IPointProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 积分商品服务实现类
 */
@Slf4j
@Service
public class PointProductServiceImpl extends ServiceImpl<PointProductMapper, PointProduct> implements IPointProductService {
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createProduct(PointProduct product) {
        // 设置默认值
        if (product.getStock() == null) {
            product.setStock(0);
        }
        if (product.getTotalExchanged() == null) {
            product.setTotalExchanged(0);
        }
        if (product.getIsHot() == null) {
            product.setIsHot(false);
        }
        if (product.getSortOrder() == null) {
            product.setSortOrder(0);
        }
        if (product.getStatus() == null) {
            product.setStatus("active");
        }
        
        this.save(product);
        log.info("创建积分商品成功: {}", product.getId());
        return product.getId();
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProduct(PointProduct product) {
        this.updateById(product);
        log.info("更新积分商品成功: {}", product.getId());
    }
    
    @Override
    public Page<PointProduct> getProductList(Integer page, Integer pageSize, String category, String status) {
        LambdaQueryWrapper<PointProduct> wrapper = new LambdaQueryWrapper<>();
        
        if (category != null && !category.isEmpty()) {
            wrapper.eq(PointProduct::getCategory, category);
        }
        
        if (status != null && !status.isEmpty()) {
            wrapper.eq(PointProduct::getStatus, status);
        }
        
        wrapper.orderByDesc(PointProduct::getSortOrder)
               .orderByDesc(PointProduct::getCreatedAt);
        
        return this.page(new Page<>(page, pageSize), wrapper);
    }
    
    @Override
    public Page<PointProduct> getHotProducts(Integer page, Integer pageSize) {
        LambdaQueryWrapper<PointProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PointProduct::getIsHot, true)
               .eq(PointProduct::getStatus, "active")
               .orderByDesc(PointProduct::getSortOrder)
               .orderByDesc(PointProduct::getTotalExchanged);
        
        return this.page(new Page<>(page, pageSize), wrapper);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void decreaseStock(Long productId, Integer quantity) {
        PointProduct product = this.getById(productId);
        if (product == null) {
            throw new RuntimeException("商品不存在");
        }
        
        if (product.getStock() < quantity) {
            throw new RuntimeException("库存不足");
        }
        
        product.setStock(product.getStock() - quantity);
        product.setTotalExchanged(product.getTotalExchanged() + quantity);
        
        // 检查库存，自动更新状态
        if (product.getStock() == 0) {
            product.setStatus("out_of_stock");
        }
        
        this.updateById(product);
        log.info("商品库存减少: ID={}, 数量={}, 剩余={}", productId, quantity, product.getStock());
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void increaseStock(Long productId, Integer quantity) {
        PointProduct product = this.getById(productId);
        if (product == null) {
            throw new RuntimeException("商品不存在");
        }
        
        product.setStock(product.getStock() + quantity);
        
        // 恢复上架状态
        if ("out_of_stock".equals(product.getStatus())) {
            product.setStatus("active");
        }
        
        this.updateById(product);
        log.info("商品库存增加: ID={}, 数量={}, 剩余={}", productId, quantity, product.getStock());
    }
}

